<?php
/*
 * @Author: your name
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2022-03-06 23:10:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\crm;

// use App\Models\admin_model\Admins;                        // admins表
use App\Models\crm_model\CrmColumn;                        // admins表
use App\Http\Controllers\crm\Login;
// use App\Http\Controllers\redis\RedisController as Redis;  //redis 缓存

class CheckRole extends BaseController
{
    /**
     * @description: 登录接口
     */
    public function checkRole()
    {
        $admin = $this->getAdminInfo();
        if ($admin['status']  == 0) {
            return $this->create(-1, '该账号已被禁用');
        } else if ($admin['delete_time']) {
            return $this->create(-1, '该账号已被删除');
        } else if (!$admin['division_id']) {
            $loginObj = new Login;
            $loginObj->loginOut(false);   //退出登录
            return $this->create(-1, '该账号没有选择所属部门');
        }
        // if ($admin['division_id'] == 0) {   //0为超级管理员
        //     $permission = CrmColumn::permissionColumn();
        //     $admin['division_name'] = '全公司';  //部门名称
        // } else {
        $division = redis_mysql_selone_return('crm_division', $admin['division_id'], ['crm_role_id', 'name', 'pid']);
        $permission = CrmColumn::permissionList($division['crm_role_id']);
        $admin['division_name'] = $division['name'];            //部门名称
        $admin['division_pid']  = $division['pid'];    //父级部门id
        if ($division['pid']) {
            $division_pid = redis_mysql_selone_return('crm_division', $division['pid'], ['name']);
        }
        $admin['division_pid_name'] = $division_pid['name'] ?? ''; //父级部门名称
        return $this->create(200, '查询成功', [
            'avatar' =>  $admin['avatar'],
            'name' =>  $admin['nickName'],
            'introduction' => 'I am a super administrator',
            'online_msg' => $admin,
            'roles' => ['admin'],
            'permission' =>  $permission,
            // 'division_name' => $division_name
        ]);
    }


    // public function getRoute()
    // {
    //     $columnAll =  CrmColumn::columnAll();
    //     // print_r( $columnAll);
    //     $columnList =  $this->getTree( $columnAll);

    //     // if(isset($_POST['HTTP_SERVER'])){
    //     //     $_POST['HTTP_SERVER']->cookie(session_name(),session_id(),time() + 99 * 365 * 24 * 3600,'/');
    //     // }

    //     return $this->create(200, '后台还在登录中...', $columnList);
    // }





    // private function getTree($column)
    // {
    //     $column_tmp = [];
    //     $column_last = [];
    //     foreach ($column as $val) {
    //         $val['authority'] = [
    //             'permission' => $val['router'],
    //         ];
    //         $column_tmp[$val['id']] = $val;
    //     }
    //     foreach ($column_tmp as  &$val) {
    //         if ($val['pid'] == 0) {
    //             $column_last[] = &$val;
    //         } else {
    //             $column_tmp[$val['pid']]['children'][] =  &$val;
    //             $sort = array_column($column_tmp[$val['pid']]['children'], 'sort');              //提取数组某个字段
    //             array_multisort($sort, SORT_NUMERIC, $column_tmp[$val['pid']]['children']);     //按某个字段降序
    //         }
    //     }
    //     $sort = array_column($column_last, 'sort');              //提取数组某个字段
    //     array_multisort($sort, SORT_NUMERIC, $column_last);      //按某个字段降序
    //     return   $column_last;
    // }

    /**
     * @description:     前端权限格式
     * @param  array $roleArray
     * @return $permissions
     */
    //  public static function format($roleArray){
    // $role = Column::checkRole(!empty($roleArray['column_id'])?$roleArray['column_id']:'', !empty($roleArray['permission_id'])?$roleArray['permission_id']:'' );
    // $permissions = [];
    // for ($i = 0; $i < count($role); $i++) {
    //     $tmp = [];
    //     $tmp['id'] = $role[$i]['router'];
    //     $tmp['operation'] = explode(',', $role[$i]['operation']);
    //     if (!empty($role[$i]['operation_router'])) {
    //         $operation_router   = explode(',', $role[$i]['operation_router']);
    //         for ($j = 0; $j < count($operation_router); $j++) {
    //             $permissions[]['id'] =   $operation_router[$j];
    //         }
    //     }
    //     $permissions[] = $tmp;
    // }

    // return  $permissions;
    //  }

}
